package qu.sol2bpel.services;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import qu.sol2bpel.workflow.Config;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class MetadataStoreConnection {

	private Connection connect = null;
	private Statement statement = null;
	private ResultSet resultSet = null;

	public  MetadataStoreConnection(String url) throws Exception {

		// TODO Auto-generated constructor stub
		try {

			Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
			connect = DriverManager
					.getConnection(url);
		}
		catch (Exception e) {
			throw e;
		} finally {
			//close();
		}


	}


	public boolean addMatrixResource(Service_WSDL resource)

	{
		if(existsMatrixResource(resource.getUrl())==false)
		{	Gson gson = new Gson();

		String resource_json = gson.toJson(resource);

		PreparedStatement statement;
		try {
			String query= "INSERT INTO resources VALUES ('"+resource.getUrl()
					+"','"+resource_json+"')";
			System.out.println(query);
			statement = connect.prepareStatement(query);
			return  statement.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		return false;
	}

	public int updateMatrixResource(Service_WSDL resource)

	{
		Gson gson = new Gson();

		String resource_json = gson.toJson(resource);

		PreparedStatement statement;
		try {
			
			String query= "UPDATE resources SET resource ='"+resource_json+"' WHERE resource_id='"+ resource.getUrl()+"'";
			System.out.println(query);
			statement = connect.prepareStatement(query);
			return  statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return 0;
	}

	public String getMatrixResource(String resourceID)

	{
		PreparedStatement statement;
		try {
			String query  = "SELECT * FROM resources";// WHERE RESOURCE_ID='"+resourceID+"'";
			statement = connect.prepareStatement(query);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				String resource_description = resultSet.getString("resource");
				//System.out.println("Resource: " + resource_description);
				Gson gson = new Gson();
				GsonBuilder builder = new GsonBuilder();
				//builder.registerTypeAdapter(ResourceMeta.class   , new ResourceMetaAdapter());
				gson = builder.create();

				Service_WSDL m  = new Service_WSDL(gson.fromJson(resource_description, Service_WSDL.class));
				if(m.getUrl().equals(resourceID))
					return resource_description;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public boolean existsMatrixResource(String resourceURL)

	{
		PreparedStatement statement;
		try {
			String query  = "SELECT * FROM resources WHERE RESOURCE_ID='"+resourceURL+"'";
			statement = connect.prepareStatement(query);
			resultSet = statement.executeQuery();
			if(resultSet.next()) {
				
					return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public void close() {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (connect != null) {
				connect.close();
			}
		} catch (Exception e) {

		}
	}
	
	public static void main(String[] args) throws Exception
	{
		Config conf = new Config();
		MetadataStoreConnection mconn = new MetadataStoreConnection(Config.METADATASTORE_URL);
		//mconn.addMatrixResource(new Matrix(new Location("hdfs://10.160.2.24:54310/Inputs/A"), new MatrixMeta(100,100,"decimal","A")));
		
	}
}